
library(tidyverse)
library(rio)

# set replication folder as working directory
setwd("~replication")

load("data_genderedcost_background.rdata")

# only include completed answers - and answers given before deadline
# 2021-12-20 21:49:52 was the last response within the time frame
df_background <- df_background %>% 
  filter(SurveyStatus==2)

df_background <- df_background %>% 
  filter(SurveyEndTime<="2021-12-20 21:49:52")

#################################
##### FIGURES IN APPENDIX C #####
#################################


###############
##### AGE #####
###############

sample_shares <- df_background %>%
  count(age) %>% 
  mutate(prop_age = prop.table(n)) %>% 
  mutate(sex = factor("Full Sample"))

subset_shares <- df_background %>% 
  count(sex, age) %>% 
  group_by(sex) %>% 
  mutate(prop_age = prop.table(n)) %>% 
  mutate(sex = factor(ifelse(sex=="Man","Men","Women")))

shares <- bind_rows(sample_shares, subset_shares)

shares %>% 
  ggplot(data=., aes(x=age, y=prop_age, fill = sex)) +
  geom_col(color = "black", position = position_dodge2(width = 0.2)) +
  theme_bw() +
  ylab("Share") +
  xlab("Age of candidate") +
  scale_fill_grey("") +
  scale_color_grey("") +
  scale_x_continuous(breaks = seq(20,80,10), labels = seq(20,80,10)) +
  facet_wrap(~sex, ncol = 1) +
  theme(legend.position = "none", panel.background = element_rect(fill = "white"),
        strip.background = element_rect("white"),
        strip.text = element_text(hjust = 0, face = "bold"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank())

ggsave("figureC1.pdf", height = 4, width = 8)

#####################
##### EDUCATION #####
#####################

sample_shares <- df_background %>%
  count(education) %>% 
  mutate(prop_edu = prop.table(n)) %>% 
  mutate(sex = factor("Full Sample"))

subset_shares <- df_background %>% 
  count(sex, education) %>% 
  group_by(sex) %>% 
  mutate(prop_edu = prop.table(n)) %>% 
  mutate(sex = factor(ifelse(sex=="Man","Men","Women")))

shares <- bind_rows(sample_shares, subset_shares) %>%
  mutate(education = as.character(education)) %>% 
  mutate(education = case_when(education=="Short, higher education (>3 years)"~"Short, higher\n(>3 years)",
                               education=="Bachelor/professional degree"~"Professional or\nbachelor degree",
                               TRUE~education)) %>% 
  mutate(education = factor(education, levels = c("Primary School", "High School", "Vocational",
                                                  "Short, higher\n(>3 years)",
                                                  "Professional or\nbachelor degree",
                                                  "Long, higher", "Other")))


shares %>% 
  ggplot(data=., aes(x=education, y=prop_edu, fill = sex)) +
  geom_col(color = "black", position = position_dodge2(width = 0.2)) +
  theme_bw() +
  ylab("Share") +
  xlab("") +
  scale_fill_grey("") +
  scale_color_grey("") +
  coord_cartesian(ylim=c(0,0.4)) +
  geom_text(aes(label = round(prop_edu,digits = 2)),
            position = position_dodge(width = 0.9), vjust = -0.5, size = 3) +
  theme(legend.position = "right",
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank())

ggsave("figureC2.pdf", height = 4, width = 8)

##########################
##### MARITAL STATUS #####
##########################

sample_shares <- df_background %>%
  count(marital_status) %>% 
  mutate(prop_marital = prop.table(n)) %>% 
  mutate(sex = factor("Full Sample"))

subset_shares <- df_background %>% 
  count(sex, marital_status) %>% 
  group_by(sex) %>% 
  mutate(prop_marital = prop.table(n)) %>% 
  mutate(sex = factor(ifelse(sex=="Man","Men","Women")))

shares <- bind_rows(sample_shares, subset_shares)

shares %>% 
  ggplot(data=., aes(x=marital_status, y=prop_marital, fill = sex)) +
  geom_col(color = "black", position = position_dodge2(width = 0.2)) +
  theme_bw() +
  ylab("Share") +
  xlab("") +
  scale_fill_grey("") +
  scale_color_grey("") +
  geom_text(aes(label = round(prop_marital,digits = 2)),
            position = position_dodge(width = 0.9), vjust = -0.5, size = 3) +
  coord_cartesian(ylim=c(0,0.7)) +
  scale_y_continuous(labels = seq(0,0.7,0.1), breaks = seq(0,0.7,0.1)) +
  theme(legend.position = "right",
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank())

ggsave("figureC3.pdf", height = 4, width = 8)


#########################
##### WORKING HOURS #####
#########################

sample_shares <- df_background %>%
  mutate(working_hours = ifelse(working_hours>50,50,working_hours)) %>% 
  count(working_hours) %>% 
  mutate(prop_hours = prop.table(n)) %>% 
  mutate(sex = factor("Full Sample"))

subset_shares <- df_background %>% 
  mutate(working_hours = ifelse(working_hours>50,50,working_hours)) %>% 
  count(sex, working_hours) %>% 
  group_by(sex) %>% 
  mutate(prop_hours = prop.table(n)) %>% 
  mutate(sex = factor(ifelse(sex=="Man","Men","Women")))

shares <- bind_rows(sample_shares, subset_shares)

shares %>%
  mutate(working_hours = ifelse(working_hours>50,50,working_hours)) %>% 
  ggplot(data=., aes(x=working_hours, y=prop_hours, fill = sex)) +
  geom_col(color = "black", position = position_dodge2(width = 0.2)) +
  theme_bw() +
  ylab("Share") +
  xlab("Hours pr. week") +
  scale_fill_grey() +
  scale_color_grey() +
  facet_wrap(~sex, ncol = 1) +
  theme(legend.position = "none", panel.background = element_rect(fill = "white"),
        strip.background = element_rect("white"),
        strip.text = element_text(hjust = 0, face = "bold"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank())

ggsave("figureC4.pdf", height = 4, width = 8)


#################################
##### ELECTORAL PERFORMANCE #####
#################################

sample_shares <- df_background %>%
  count(electoral_performance) %>% 
  mutate(prop_elec = prop.table(n)) %>% 
  mutate(sex = factor("Full Sample"))

subset_shares <- df_background %>% 
  count(sex, electoral_performance) %>% 
  group_by(sex) %>% 
  mutate(prop_elec = prop.table(n)) %>% 
  mutate(sex = factor(ifelse(sex=="Man","Men","Women")))

shares <- bind_rows(sample_shares, subset_shares) %>% 
  mutate(electoral_performance = case_when(electoral_performance=="Not elected, but elected previously"~"Not elected, but \nelected previously",
                                           electoral_performance=="Not elected, not elected previously"~"Not elected, not \nelected previously",
                                           TRUE~electoral_performance))

shares %>% 
  ggplot(data=., aes(x=electoral_performance, y=prop_elec, fill = sex)) +
  geom_col(color = "black", position = position_dodge2(width = 0.2)) +
  theme_bw() +
  ylab("Share") +
  xlab("") +
  scale_fill_grey("") +
  scale_color_grey("") +
  scale_y_continuous(labels = seq(0,0.7,0.1), breaks = seq(0,0.7,0.1)) +
  coord_cartesian(ylim=c(0,0.6)) +
  geom_text(aes(label = round(prop_elec, digits = 2)),
            position = position_dodge(width = 0.9), vjust = -0.5, size = 3) +
  theme(legend.position = "right",
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank())

ggsave("figureC5.pdf", height = 4, width = 8)

